package com.chunuo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chunuo.eduservice.entity.EduChapter;
import com.chunuo.eduservice.entity.EduVideo;
import com.chunuo.eduservice.entity.vo.EduChapterVo;
import com.chunuo.eduservice.mapper.EduChapterMapper;
import com.chunuo.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunuo.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 找到课程中的所有章节和小节
     * @param id
     * @return
     */
    @Override
    public List<EduChapterVo> findDescription(String id) {
        List<EduChapter> eduChapterList = this.list(new QueryWrapper<EduChapter>().eq("course_id", id));
        List<EduChapterVo> collect = eduChapterList.stream().map(item -> {
            EduChapterVo vo = new EduChapterVo();
            BeanUtils.copyProperties(item, vo);
            vo.setEduVideoList(eduVideoService.list(new QueryWrapper<EduVideo>().eq("chapter_id", vo.getId()).eq("course_id",id)));
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void delete(String id) {
        this.remove(new QueryWrapper<EduChapter>().eq("course_id",id));
    }
}
